#include "dev_interface.h"
#include "parameter.h"

static interface_config_t const interface_config = {
	.magic = PARAMETER_INTERFACE_MAGIC,
	.version = CONFIG_INTERFACE_PARAMETER_VERSION,
	.size = CONFIG_INTERFACE_PARAMETER_SIZE,
	.s3_flag_source = CONFIG_S3_FLAG_SOURCE,
	.qspi_speed_rank = CONFIG_S3_FRQ_RANK,
};

static interface_config_t interface_info_l;
static interface_config_t const * const interface_ptr = &interface_info_l;

int dev_interface_config(FILE *const fp, pm_info_t const * pm_d)
{
     if(NULL == fp){
        printf("fp is point to NULL\n");
		return -1;
    }

	fseek(fp, PM_DEV_INTERFACE_OFFSET, 0);
	fwrite(&interface_config, sizeof(interface_config_t), 1, fp);
	return 0;
}

int check_dev(FILE * const fp, uint64_t base)
{
	uint32_t magic;
	fseek(fp, base + PM_DEV_INTERFACE_OFFSET, 0);
	fread(&magic, sizeof(magic), 1, fp);

	if(PARAMETER_INTERFACE_MAGIC != magic)
		return -1;
	else
		return 0;
}

char * power_ctr_source(int source)
 {
	switch(source)
	{
	case 0:
		return ("CPLD");
		break;
	case 1:
		return ("EC");
		break;
	default:
		return ("ERROR");
		break;
	}
}

char * qspi_speed_rank(int speed)
 {
	switch(speed)
	{
	case 0:
		return ("0 - 37.5MHZ");
		break;
	case 1:
		return ("1 - 18.5MHZ");
		break;
	case 2:
		return ("2 - 9.37MHZ");
		break;
	case 3:
		return ("3 - 4.6MHZ");
		break;
	default:
		return ("ERROR");
		break;
	}
}

int get_dev_info(FILE *const fp, parameter_base_t const *p_base)
{
    if(NULL == fp){
		ERROR("fp is point NULL %s \n", __func__);
		return -1;
	}

	fseek(fp, p_base->addr + PM_DEV_INTERFACE_OFFSET, 0);
	fread(&interface_info_l,sizeof(interface_config_t),1,fp);

    if(PARAMETER_INTERFACE_MAGIC != interface_ptr->magic){
		ERROR("can not get right magic 0x%x, %s \n", interface_ptr->magic, __func__);
		return -1;
	}

	printf("=======================================\n");

	printf("DEV INTERFACE info: \n");
	printf("version = 0x%x\n", interface_ptr->version);

	printf("S3 flag: %s\n", power_ctr_source(interface_ptr->s3_flag_source));
	printf("QSPI speed: %s\n", qspi_speed_rank(interface_ptr->qspi_speed_rank));

	printf("=======================================\n");

	return 0;
}

pm_list_fun_t dev_fun = {
	.cfg_fnc = dev_interface_config,
	.ck_fnc = check_dev,
	.prf_fnc = get_dev_info,
};